package com.cskaoyan.controller.admin.mallcontroller;

import com.cskaoyan.Utils.LogUtils;
import com.cskaoyan.Utils.MyUtils;
import com.cskaoyan.service.mallservice.OrderService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.mallvo.OrderDetailVo;
import com.cskaoyan.vo.mallvo.OrderListVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @author hsp
 * @date 2021/5/10 16:01
 */
@RestController
@RequestMapping("admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;
    @Autowired
    LogUtils logUtils;
    /**
     * page: 1
     * limit: 20
     * orderStatusArray: 103
     * orderStatusArray: 201
     * sort: add_time
     * order: desc
     * userId: 1
     * orderSn: 20210424759808
     * @return
     */
    @RequiresPermissions("admin:order:list")
    @RequestMapping("list")
    public BaseRespVo list(Integer page,Integer limit,Integer[] orderStatusArray
            ,String sort,String order,Integer userId,String orderSn ){
        /*
           private long total;
           private List<OrderVo> items;
         */
       OrderListVo orderListVo =  orderService.queryOrder(page,limit,orderStatusArray,sort,order,userId,orderSn);

        return BaseRespVo.ok(orderListVo);

    }

    @RequiresPermissions("admin:order:read")
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id){

       OrderDetailVo orderDetailVo =  orderService.getDetailId(id);


       return BaseRespVo.ok(orderDetailVo);

    }

    @RequiresPermissions("admin:order:refund")
    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        Integer refundMoney = (Integer) map.get("refundMoney");
        orderService.refund(orderId,refundMoney);

        return BaseRespVo.ok();
    }

    @RequiresPermissions("admin:order:ship")
    @RequestMapping("ship")
    public BaseRespVo ship(@RequestBody Map map, HttpServletRequest request){
        Integer orderId = (Integer) map.get("orderId");
        String shipChannel = (String) map.get("shipChannel");
        String shipSn = (String) map.get("shipSn");
        //发货
        orderService.ship(orderId,shipChannel,shipSn);
        logUtils.insertLog(MyUtils.getAdminname(),request.getRemoteAddr(),new Date(),2,"发货",true,"订单编号"+orderId);
        return BaseRespVo.ok();

    }
}
